var pre_rule = JSON.parse('{"CODE":0, "ACTION":"","LEVEL":"T1", "MSG":"","DATA":"","SCRIPTNAME":"pre_rule"}');

// var iva_tax = [];
// var vat_in_tax = [];
var item_store_status = [];
var vnpk_qty_list = [];
var item_store_range = [];
var item_store_price = [];
var way_order_list = [];

if (!isNull(item_store_json)) {
    // iva_tax = getUniqueValues(item_store_json, "iva_tax");
    // vat_in_tax = getUniqueValues(item_store_json, "vat_in_tax");
    item_store_status = getUniqueValues(item_store_json, "item_store_status");
    vnpk_qty_list = getUniqueValues(item_store_json, "vnpk_qty");
    item_store_range = getUniqueValues(item_store_json, "store_id");
    // item_store_price = getUniqueValues(item_store_json, "store_id");
    way_order_list = getUniqueValues(item_store_json, "way_order");
}

// 获取门店城市对应关系
var store_trait = new Map();
var trait_city_name_map = new Map();
// 获取门店JD对应关系
var store_jd = new Map();
// 获取城市门店对应关系 多个为数组
var trait_store = new Map();
var trait_store_obj = [];
// 所有jd店
var all_store_jd = [];
var store_trait_url = "https://mdc-qa-aloha-gateway.cn.wal-mart.com/price-change/external/cityClubList";
var store_trait_result = JSON.parse(func_post(store_trait_url, {}));


for (var i = 0; i < store_trait_result.result.length; i++) {
    var resultElement = store_trait_result.result[i];
    store_trait.put(resultElement.club, resultElement.cityCode);
    if (!isNull(resultElement.jdstore)) {
        store_jd.put(resultElement.club, resultElement.jdstore);
        var jdstoreSplit = resultElement.jdstore.split(',')
        for (var jdindex in jdstoreSplit) {
            all_store_jd.push(jdstoreSplit[jdindex]);
        }
    }
    var flg = false;
    for (var elm in trait_store_obj) {
        if (trait_store_obj[elm].cityCode === resultElement.cityCode) {
            flg = true;
            trait_store_obj[elm].club.push(resultElement.club);
        }
    }
    if (!flg) {
        var obj = {
            cityCode: resultElement.cityCode,
            club: [resultElement.club]
        }
        trait_store_obj.push(obj);
    }
    trait_city_name_map.put(resultElement.cityCode, resultElement.city);
}

for (var i in trait_store_obj) {
    trait_store.put(trait_store_obj[i].cityCode, trait_store_obj[i].club)
}

//pre_rule.MSG = all_store_jd;
var currentTraitToStoreList = [];
if (!isNull(trait_nbr) && !isNull(trait_store)) {
    if (trait_nbr.toUpperCase() === "ALL") {
        var allTraitStoreList = trait_store.values();
        for (var allTr1 = 0; allTr1 < allTraitStoreList.length; allTr1++) {
            var storeListElement = allTraitStoreList[allTr1];
            currentTraitToStoreList = currentTraitToStoreList.concat(storeListElement);
        }
        // currentTraitToStoreList = allTraitStoreList;
    } else {
        var currentTraitStore = trait_store.get(trait_nbr);
        currentTraitToStoreList = currentTraitToStoreList.concat(currentTraitStore);
    }
} else if (!isNull(store_id)) {
    currentTraitToStoreList.push(store_id)
}

var frPcSkuInfo_price_change_detail_result = "";

if (!isNull(retail_tax)) {
    // 获取item变价次数
    var frPcSkuInfo_url = "https://mdc-dev-aloha-gateway.cn.wal-mart.com/price-change/frPcSkuInfo/price-change-detail";

    var frPcSkuInfo_price_change_detail_param_beforeDay = -1

    func_suggest_no_price_by_category_fr().forEach(function (s) {
        if (s == category_nbr) {
            frPcSkuInfo_price_change_detail_param_beforeDay = 14
        }
    })

    func_suggest_no_price_by_category().forEach(function (s) {
        if (s == category_nbr) {
            frPcSkuInfo_price_change_detail_param_beforeDay = 180
        }
    })
    if (frPcSkuInfo_price_change_detail_param_beforeDay != -1) {
        var frPcSkuInfo_price_change_detail_param = {
            "appName": "",
            "format": "",
            "param": {
                "requestSkuId": id,
                "statusList": [3000, 3001, 3004, 3005, 3006, 4000, 4001, 4004, 4003, 4005, 5000, 5001, 5002, 5003, 5004],
                "beforeDay": frPcSkuInfo_price_change_detail_param_beforeDay
            },
            "sign": "",
            "source": "",
            "timestamp": "",
            "version": ""
        }
        frPcSkuInfo_price_change_detail_result = JSON.parse(func_post(frPcSkuInfo_url, JSON.stringify(frPcSkuInfo_price_change_detail_param))).result;
    }
}

// 获取cate 于户信息
var category_user = new Map();
var category_user_url = "https://samspricelock-qa.cn.wal-mart.com/pricelock/gateway/spl-pricing/categoryBuyer/selectAll";
var category_user_result = func_get(category_user_url, "{ \"currentPage\": 1, \"pageSize\": 1000 }");
var category_user_result_list = JSON.parse(category_user_result).result.list;

for (var category_el in category_user_result_list) {
    var el = category_user_result_list[category_el];
    var result = {
        division: el.division,
        buyerId: el.buyerId,
        maId: el.maId,
        ma2Id: el.ma2Id,
        dmmId: el.dmmId,
        gmmId: el.gmmId,
        cmoId: el.cmoId,
        swuId: el.swuId,
        buHuoId: el.buHuoId,
        chLieId: el.chLieId,
        priceTeamId: el.priceTeamId,
        muBuyerId: el.muBuyerId,
        luYanMaId: el.luYanMaId,
        luYanBuyerId: el.luYanBuyerId,
        luYanDmmId: el.luYanDmmId,
        luYanGmmId: el.luYanGmmId,
        luYanMaId: el.luYanMaId
    }
    category_user.put(el.category, result);
}
//pre_rule.MSG = JSON.stringify(category_user.get(301));

// 商品TBFF ,PBDI或DI商品
var item_attributes = new Map();
var item_attributes_param = {
    itemNbrList: [item_nbr]
}
var item_attributes_url = "https://mdc-qa-aloha-gateway.cn.wal-mart.com/spp-price-change/server/item-relation/info";
var item_attributes_params = func_sgin(item_attributes_param, "A3D3447D09CA8AA25DF54109B208C3761", "price-change", "usercenter", "1.0.0", "json", "price-change");
var item_attributes_params_target = JSON.parse(item_attributes_params);
item_attributes_params_target.param = item_attributes_param;
var item_attributes_result = func_post(item_attributes_url, JSON.stringify(item_attributes_params_target));
var item_attributes_result_target = JSON.parse(item_attributes_result).result[0];

// 是不是 TBFF 商品
item_attributes.put("tbffFlag", item_attributes_result_target.tbffFlag);
// 是不是 pb 商品
item_attributes.put("pbdiClubDcs", item_attributes_result_target.pbdiClubDcs);
// 是不是 color
item_attributes.put("color", item_attributes_result_target.color);

var only_item_list_params = {
    "appName": "",
    "format": "",
    "param": [item_nbr],
    "sign": "",
    "source": "",
    "timestamp": "",
    "version": ""
}
var vendorBlackList = [];
var itemRestrictList = [];
if (!isNull(item_status) || !isNull(cost) || !isNull(retail_tax)) {
    var vendor_url = "https://mdc-qa-aloha-gateway.cn.wal-mart.com/spp-price-change/server/item-relation/findThirdServerInfo";
    var item_vendor_result = func_post(vendor_url, JSON.stringify(only_item_list_params));
    var item_vendor_ban = JSON.parse(item_vendor_result).result;
    //禁售列表
    if (!isNull(item_vendor_ban)) {
        item_vendor_ban.forEach(function (res) {
            var restrictList = res.restrictList
            if (!isNull(restrictList)) {
                restrictList.forEach(function (restrict) {
                    itemRestrictList.push(restrict)
                })
            }
            var vendorInfoList = res.vendorInfoList;
            if (!isNull(vendorInfoList)) {
                vendorInfoList.forEach(function (vendorInfo) {
                    vendorBlackList.push(vendorInfo)
                })
            }
        })
    }
}

var yellowLabelList = [];
var priceLockList = [];
if (!isNull(retail_tax)) {
    var priceLockUrl = "https://mdc-qa-aloha-gateway.cn.wal-mart.com/spp-price-change/server/item-relation/findYellowLabelCompPriceInfo";
    var price_lock_yellowLabel_result = func_post(priceLockUrl, JSON.stringify(only_item_list_params));
    var price_lock_yellowLabel = JSON.parse(price_lock_yellowLabel_result).result;

    if (!isNull(price_lock_yellowLabel)) {
        price_lock_yellowLabel.forEach(function (plItemYellowLabel) {
            var itemYellowLabelList = plItemYellowLabel.yellowLabelList
            if (!isNull(itemYellowLabelList)) {
                itemYellowLabelList.forEach(function (itemYellowLabel) {
                    yellowLabelList.push(itemYellowLabel)
                })
            }
            var priceLockList2 = plItemYellowLabel.priceLockList
            if (!isNull(priceLockList2)) {
                priceLockList2.forEach(function (priceLock) {
                    priceLockList.push(priceLock)
                })
            }
        })
    }
}


JSON.stringify(pre_rule);